package basic.study.leetcode;

/**
 * @ClassName Problem152
 * @Description
 * @Company inspur
 * @Author Kevin
 * @Date 2020/5/18 9:43
 * @Version 1.0
 */
public class Problem152 {
    class Solution {
        public int maxProduct(int[] nums) {
            if(nums.length == 1) return nums[0];

            int[] dp1 = new int[nums.length];//记录最大
            int[] dp2 = new int[nums.length];//记录最小

            dp1[0] = nums[0];
            dp2[0] = nums[0];
            int max = nums[0];

            for(int i = 1; i < nums.length; i++ ) {
                dp1[i] = Math.max(nums[i], Math.max(nums[i]*dp1[i-1], nums[i]*dp2[i-1]));
                dp2[i] = Math.min(nums[i], Math.min(nums[i]*dp1[i-1], nums[i]*dp2[i-1]));
                max = Math.max(max, dp1[i]);
            }
            return max;
        }
    }
}
